package com.googlecode.bip.util;

import java.io.Serializable;
import java.util.EnumSet;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import com.googlecode.bip.entity.Roles;

/**
 * Helper to identify roles
 *
 * @author Claude CHOISNET
 */
public class RolesIdentifier implements Serializable
{
    private static final long serialVersionUID = 1L;
    private Set<Roles> roles;

    /**
     * All existing {@link Roles} : {@value}
     */
    public static final Set<Roles> ROLES = EnumSet.allOf( Roles.class );

    public RolesIdentifier( final Set<Roles> roles )
    {
        this.roles = roles;
    }

    public boolean isUser()
    {
        return this.roles.contains( Roles.BIP_USER );
    }

    public static boolean isUser(
            final HttpServletRequest request
            )
    {
        return isUserInRole( request, Roles.BIP_USER );
    }

    public boolean isReferent()
    {
        return this.roles.contains( Roles.BIP_REF );
    }

    public static boolean isReferent(
            final HttpServletRequest request
            )
    {
        return isUserInRole( request, Roles.BIP_REF );
    }

    public boolean isAuthor()
    {
        return this.roles.contains( Roles.BIP_AUTHOR );
    }

   public static boolean isAuthor(
        final HttpServletRequest request
        )
    {
        return isUserInRole( request, Roles.BIP_AUTHOR );
    }

    public boolean isAdministrator()
    {
        return this.roles.contains( Roles.BIP_ADMIN );
    }

    public static boolean isAdministrator(
            final HttpServletRequest request
            )
    {
        return isUserInRole( request, Roles.BIP_ADMIN );
    }

    /**
     * Returns {@link Set} of all defined {@link Roles}
     * @return {@link Set} of all defined {@link Roles}
     */
    public static Set<Roles> getRoles()
    {
        return ROLES;
    }

    /**
     * 
     * @param request
     * @param role
     * @return
     */
    public static boolean isUserInRole(
            final HttpServletRequest request,
            final Roles              role
            )
    {
        return request.isUserInRole( role.name() );
    }
    
}
